#!/bin/sh

ARGS=$(getopt -o a:p:t: --long "system,user" -- "$@");

if [ $? -ne 0 ];
then
  exit 1
fi

eval set -- "$ARGS";

while true; do
    case "$1" in
        -a)
            action=$2
            shift 2;
            ;;
        -p)
            path=$2
            shift 2;
            ;;
        -t)
            type=$2
            shift 2;
            ;;
        --system)
            params=--system;
            shift;
            ;;
        --user)
            params=;
            shift;
            ;;
        --)
            shift;
            ;;
        *)
            if [ $# -eq 0 ]; then
                break;
            fi

            if [ $# -lt 3 ]; then
                echo "$0: unused params: $@"
                break;
            fi

            action=$1;
            path=$2;
            type=$3;
            shift 3;
            if [ $# -eq 1 ]; then
                if [ "$1" == "system" ]; then
                    params=--system;
                elif [ "$1" == "user" ]; then
                    params=;
                fi
            elif [ $# -gt 1 ]; then
                echo "$0: unused params: $@"
            fi
            break;
            ;;
    esac
done

if [ ! -n "$action" ]; then
    echo "Need action"
    exit 1
fi
if [ ! -n "$path" ]; then
    echo "Need provider path"
    exit 1
fi
if [ ! -n "$type" ]; then
    echo "Need provider type"
    exit 1
fi

if [ ! -f "$path" ]; then
    echo "Provider file does not exist: $path"
    exit 1
fi

LC_ALL=C statefs $action $path --statefs-type=$type $params
